define(function(require,exports,module) {
	var o={
		table:null,
		init:function(){
			var table=new Table({
				render_id:"${classNameLower}_render_id",
				page_render_id:"${classNameLower}_render_page_id",
				column:[
					{
						field:"id",
						title:"<input type='checkbox' name=''/>",
						escape:false,
						format:function(row,i){
							return "<input type='checkbox' name='' value='"+row.id+"'/>";
						}
					},
					<#list colList as col>
					{
						field:"${col.columnName}",
						title:"${col.remarks}",
						format:function(row,i){
							return row.${col.columnName};
						}
					},
					</#list>
					{
						field:"id",
						title:"操作",
						escape:false,
						format:function(row,i){
							return "<a title='编辑' id='editor' val='"+row.id+"' href='javascript:;' class='ml-5' style='text-decoration:none'><i class='ui-iconfont'>&#xe6df;</i></a> <a title='详情' id='detail' val='"+row.id+"' href='javascript:;' class='ml-5' style='text-decoration:none'><i class='ui-iconfont'>&#xe72d;</i></a> <a title='删除' id='del' val='"+row.id+"' href='javascript:;' class='ml-5' style='text-decoration:none'><i class='ui-iconfont'>&#xe6e2;</i></a>";
						}
					}
				],
			});

			table.load(function(callback,param){
				app.log("table load:"+JSON.stringify(param));
				$.ajax({
					type:"POST",
					dataType:"json",
					data:param,
					url:Config.root+"/${classNameLower}/list",
					success:function(data){
						callback(data);
					},
					error:function(){
					}
				});
			});
			this.table=table;
		},
		bindEvent:function(){
			app.log("bindEvent");
			var _this=this;
			$("#add").click(function(){
				app.log("add Click");
				Dao.html(Config.root+"/${classNameLower}_add.html",function(content){
						var index= layer.open({
							 type: 1,
							 area: ['800px', '500px'],
							 shadeClose: true, 
    						 content:content
						});
						_this.addEvent(index);
				});
				return false;
			});
			
			$("#search").click(function(){
				var startTime=$("#startTime").val();
				var endTime=$("#endTime").val();
				var param={"startTime":startTime,"endTime":endTime};
				_this.table.search(param);
			});

			$("#refresh_id").click(function(){
				_this.table.reload();
			});

			$("#${classNameLower}_render_id").on("click","#editor",function(){
				var val=$(this).attr("val");
				Dao.html(Config.root+"/${classNameLower}_update.html",function(content){
					var index= layer.open({
							 type: 1,
							 area: ['800px', '500px'],
							 shadeClose: true, 
    						 content:content
						});

					$.ajax({
						url:"/${classNameLower}/detail/"+val,
						type:"POST",
						dataType:"json",
						success:function(data){
							if(data.success){
								var result=data.result;
								<#list colList as col>
									$("#${col.columnName}").val(result.${col.columnName});
								</#list>
							}
							
							_this.updateEvent();
							
						},
						error:function(){
						}
					});
				});
			});
			
			$("#${classNameLower}_render_id").on("click","#detail",function(){
				var val=$(this).attr("val");
				Dao.html(Config.root+"/${classNameLower}_detail.html",function(content){
					var index= layer.open({
							 type: 1,
							 area: ['800px', '500px'],
							 shadeClose: true, 
    						 content:content
						});

					$.ajax({
						url:"/${classNameLower}/detail/"+val,
						type:"POST",
						dataType:"json",
						success:function(data){
							if(data.success){
								var result=data.result;
								<#list colList as col>
									$("#${col.columnName}").val(result.${col.columnName});
								</#list>
							}
						},
						error:function(){
						}
					});
				});
			});

			$("#${classNameLower}_render_id").on("click","#del",function(){
				var val=$(this).attr("val");
				layer.confirm("确认删除?",{icon:3,title:"提示"},function(index){
					$.ajax({
						url:"/${classNameLower}/del/"+val,
						type:"POST",
						dataType:"json",
						data:{},
						success:function(data){
							layer.close(index);
							if(data.success){
								layer.alert("删除成功",{icon: 1});
							}else{
								layer.alert("删除失败",{icon: 2});
							}

							_this.table.reload();
						},
						error:function(){
							layer.close(index);
						}
					});		
				});
			});
		},
		addEvent:function(index){
			var _this=this;
			$("#${classNameLower}_id").validate({
				debug:true,
				errorClass:"form-label error",
				rules:{
					<#list colList as col>
					 <#if col_has_next>
					 ${col.columnName}: "required", 
					 <#else>
					 ${col.columnName}: "required"
					 </#if>
					</#list>
				},
				messages:{
					<#list colList as col>
					 <#if col_has_next>
					 ${col.columnName}: "不能为空", 
					 <#else>
					 ${col.columnName}: "不能为空"
					 </#if>
					</#list>
				},
				errorPlacement:function(error,element){
					error.appendTo(element.parent().parent());
				},
				submitHandler:function(){
					$("#sub_add_btn").attr("disabled", true);
					<#list colList as col>
					var ${col.columnName}=$("#${col.columnName}").val();
					</#list>
					$.ajax({
						type:"POST",
						dataType:"json",
						url:"/${classNameLower}/add",
						data:{
							<#list colList as col>
							<#if col_has_next>
							"${col.columnName}" : ${col.columnName},
							<#else>
							"${col.columnName}" : ${col.columnName}
							</#if>
							</#list>
						},
						success:function(data){
							$("#sub_add_btn").attr("disabled", false);
							if(data.success){
								layer.alert("添加成功",{icon: 1});
							}else{
								layer.alert("添加失败",{icon: 2});
							}
							layer.close(index);
							_this.table.reload();
						},
						error:function(){
						}
					});

				}
			});

			$("#${classNameLower}_add_cancel").click(function(){
				layer.close(index);
			});
		},
		updateEvent:function(){
			$("#${classNameLower}_id").validate({
				debug:true,
				errorClass:"form-label error",
				rules:{
					<#list colList as col>
					 <#if col_has_next>
					 ${col.columnName}: "required", 
					 <#else>
					 ${col.columnName}: "required"
					 </#if>
					</#list>
				},
				messages:{
					<#list colList as col>
					 <#if col_has_next>
					 ${col.columnName}: "不能为空", 
					 <#else>
					 ${col.columnName}: "不能为空"
					 </#if>
					</#list>
				},
				errorPlacement:function(error,element){
					error.appendTo(element.parent().parent());
				},
				submitHandler:function(){
					$("#sub_update_btn").attr("disabled", true);
					<#list colList as col>
					var ${col.columnName}=$("#${col.columnName}").val();
					</#list>
					$.ajax({
						type:"POST",
						dataType:"json",
						url:"/${classNameLower}/update",
						data:{
							<#list colList as col>
							<#if col_has_next>
							"${col.columnName}" : ${col.columnName},
							<#else>
							"${col.columnName}" : ${col.columnName}
							</#if>
							</#list>
						},
						success:function(data){
							$("#sub_update_btn").attr("disabled", false);
							if(data.success){
								layer.alert("更新成功");
							}else{
								layer.alert("更新失败");
							}
							layer.close(index);
							_this.table.reload();
						},
						error:function(){
						}
					});
				}
			});

			$("#${classNameLower}_update_cancel").click(function(){
				layer.close(index);
			});
		}
	}
	return o;
});
